//my glue package
package glue;

import java.awt.*;
import java.io.*;
import java.util.*;
import javax.swing.filechooser.FileFilter;

public class SuperFileFilter extends FileFilter 
{
	Utils ut = null;
	ArrayList<String> extList = null;
	String filter = null;
	String description = null;
	boolean showDirectory = true;
	boolean showFiles = true;
	
	public class Utils {
		
		public String prj = null;
		
		public Utils(String ext){
			prj = ext;
		}	
	    
	
	    public String getExtension(File f) {
	        String ext = null;
	        String s = f.getName();
	        int i = s.lastIndexOf('.');
	
	        if (i > 0 &&  i < s.length() - 1) {
	            ext = s.substring(i+1).toLowerCase();
	        }
	        return ext;
	    }
	}    
	
	public SuperFileFilter(String ext)
	{
		filter = new String(ext);
		ut = new Utils(ext);
		
		extList = new ArrayList<String>();
		parseExt(ext);
	}	
	
	public SuperFileFilter(String ext, boolean showDirectory)
	{
		this.showDirectory = showDirectory;
		filter = new String(ext);
		ut = new Utils(ext);
		
		extList = new ArrayList<String>();
		parseExt(ext);
	}
	
	public SuperFileFilter(String ext, boolean showDirectory, boolean showFiles)
	{
		this.showDirectory = showDirectory;
		this.showDirectory = showDirectory;
		filter = new String(ext);
		ut = new Utils(ext);
		
		extList = new ArrayList<String>();
		parseExt(ext);
	}
	
	public SuperFileFilter(String description, String ext)
	{
		this.description = description;
		filter = new String(ext);
		ut = new Utils(ext);
		
		extList = new ArrayList<String>();
		parseExt(ext);
	}
	

	public SuperFileFilter(String description, String ext, boolean showDirectory)
	{
		this.showDirectory = showDirectory;
		this.description = description;
		filter = new String(ext);
		ut = new Utils(ext);
		
		extList = new ArrayList<String>();
		parseExt(ext);
	}
	
/*
* Where's my "SuperFileFilter(String description, String ext, boolean showDirectory = true, boolean showFiles = true)?"
*/
	
	public SuperFileFilter(String description, String ext, boolean showDirectory, boolean showFiles)
	{
		this.showFiles = showFiles;
		this.showDirectory = showDirectory;
		this.description = description;
		filter = new String(ext);
		ut = new Utils(ext);
		
		extList = new ArrayList<String>();
		parseExt(ext);
	}
	
	public String getPossibleExtensions(){
		return filter;
	}
	
	public void parseExt(String ext){
     	String[] result = ext.split(",");
     	for (int x=0; x<result.length; x++){
     		result[x].trim();
         	extList.add(result[x]);
     	}
	}

    public boolean accept(File f) {
        if (f.isDirectory()) {
            return (showDirectory)? true : false;
        }else if(showFiles){
	        String extension = ut.getExtension(f);
	        if (extension != null) {
		        for (String s : extList){
			        if (extension.equalsIgnoreCase(s)){
				        return true;
			        }
		        }
	 	       	return false;
	        }
		}
        return false;
    }

    //The description of this filter
    public String getDescription() {
	    String str = new String();
	    for (String s : extList){
		    str += "*.";
		    str +=  s;
		    
		    if(extList.indexOf(s) < extList.size() - 1){
			    str += ", ";
		    }
	    }
	    
  	    if(description != null){
	  	    String out = new String(description + " (" + str + ")");
	  	    return out;
  	    }
        return str;
    }	
}
